package de.unibremen.mum.app.whatscool;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class ResultActivity extends Activity {
	MainActivity ma;
	private DatabaseHandler db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		ma = new MainActivity();
		db = new DatabaseHandler(this);
		long id = -1;
		id = getIntent().getLongExtra("id", id);

		TextView ueberschrift = (TextView) findViewById(R.id.textView1);
//		TextView kommentar = (TextView) findViewById(R.id.textView2);
		RatingBar r1 = (RatingBar) findViewById(R.id.ratingBarResult);
		r1.setEnabled(false);
		RatingClass rating = db.getRating(id);
		ueberschrift.setText(rating.getName());
//		kommentar.setText(db.getRating(id).getComment());
		r1.setRating(Float.parseFloat(rating.getResult()));
		createProductPic(rating.getPic());
		db.close();

		// Handler beim Klicken auf den Button
		final Button button = (Button) findViewById(R.id.backToHome);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(ResultActivity.this, MainActivity.class);
				startActivity(myIntent);
			}
		});
	}

	/**
	 * Erzeugt das Foto in der View
	 * @param url URL zum Foto
	 */
	public void createProductPic(String url) {
		if ((new File(url)).exists()) {
			Bitmap bitmap = BitmapFactory.decodeFile(url);
			if (bitmap != null) {
				ImageView iv = (ImageView) findViewById(R.id.quickContactBadge1);
				iv.setImageBitmap(bitmap);
			} else {
				Toast.makeText(this, "Foto nicht gefunden", Toast.LENGTH_SHORT).show();
			}
		}
	}

}
